package com.yanqu.road.dao.impl.drugstore;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.drugstore.DrugHardCaseUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2022/4/18 17:33
 */
public class DrugHardCaseUserDataDaoImpl extends TempDataObjectDao<DrugHardCaseUserData> {

    public static String getDrugHardCaseUserDataSql = "select * from `t_u_drug_store_hardcase_user` where `userId`=?";
    public DrugHardCaseUserData getDrugHardCaseUserData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getDrugHardCaseUserDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateDrugHardCaseUserDataSql = "update `t_u_drug_store_hardcase_user` set status=?,times=?,refreshTime=?,caseId=?,healthValue=?,fullMap=?,receiveList=? where userId=?";
    @Override
    protected boolean updateData(DrugHardCaseUserData drugHardCaseUserData) {
        drugHardCaseUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, drugHardCaseUserData.getStatus()));
        para.put(i++, new DbParameter(Types.INTEGER, drugHardCaseUserData.getTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, drugHardCaseUserData.getRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, drugHardCaseUserData.getCaseId()));
        para.put(i++, new DbParameter(Types.INTEGER, drugHardCaseUserData.getHealthValue()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drugHardCaseUserData.getFullMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drugHardCaseUserData.getReceiveList())));
        para.put(i++, new DbParameter(Types.BIGINT, drugHardCaseUserData.getUserId()));
        if (getDb().executeNoneQuery(updateDrugHardCaseUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertDrugHardCaseUserDataSql = "insert into `t_u_drug_store_hardcase_user`(userId,status,times,refreshTime,caseId,healthValue,fullMap,receiveList) values (?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(DrugHardCaseUserData drugHardCaseUserData) {
        drugHardCaseUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, drugHardCaseUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, drugHardCaseUserData.getStatus()));
        para.put(i++, new DbParameter(Types.INTEGER, drugHardCaseUserData.getTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, drugHardCaseUserData.getRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, drugHardCaseUserData.getCaseId()));
        para.put(i++, new DbParameter(Types.INTEGER, drugHardCaseUserData.getHealthValue()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drugHardCaseUserData.getFullMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(drugHardCaseUserData.getReceiveList())));
        if (getDb().executeNoneQuery(insertDrugHardCaseUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected DrugHardCaseUserData parseData(ResultSet rs) throws SQLException {
        DrugHardCaseUserData drugHardCaseUserData = new DrugHardCaseUserData();
        drugHardCaseUserData.setUserId(rs.getLong("userId"));
        drugHardCaseUserData.setStatus(rs.getInt("status"));
        drugHardCaseUserData.setTimes(rs.getInt("times"));
        drugHardCaseUserData.setRefreshTime(rs.getInt("refreshTime"));
        drugHardCaseUserData.setCaseId(rs.getInt("caseId"));
        drugHardCaseUserData.setHealthValue(rs.getInt("healthValue"));
        drugHardCaseUserData.setFullMap(JSON.parseObject(rs.getString("fullMap"), new TypeReference<Map<Integer, Integer>>() {}));
        drugHardCaseUserData.setReceiveList(JSON.parseObject(rs.getString("receiveList"), new TypeReference<java.util.List<java.lang.Integer>>() {}));

        return drugHardCaseUserData;
    }
}
